package com.mindainfo.groundwave.web.member.web;

import com.mindainfo.groundwave.common.api.member.param.PermissionQueryListParam;
import com.mindainfo.groundwave.common.api.member.param.PermissionSaveParam;
import com.mindainfo.groundwave.common.api.member.param.PermissionToDelParam;
import com.mindainfo.groundwave.common.api.member.service.IPermissionService;
import com.mindainfo.groundwave.common.api.member.vo.PermissionQueryForTreeVo;
import com.mindainfo.groundwave.common.api.member.vo.PermissionQueryListVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by MD-01 on 2017/5/17.
 */
@Controller
@RequestMapping("/sys/permission")
public class PermissionController {

    @Resource
    private IPermissionService permissionService;

    @RequestMapping("/page")
    public String page(){
        return "/member/permission";
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public TableData<PermissionQueryListVo> queryList(Integer start, Integer length, String draw){

        PermissionQueryListParam param = new PermissionQueryListParam();
        param.setStart(start);
        param.setNum(length);
        CommonResult<TableData<PermissionQueryListVo>> result = permissionService.queryList(param);
        result.getData().setDraw(draw);
        return result.getData();
    }

    @RequestMapping("/save")
    @ResponseBody
    public CommonResult<Void> save(Integer permissionId,String name,Integer parentId,
                                   String tag,String url,Integer type,Integer usable,
                                   Integer orderNum){

        PermissionSaveParam param = new PermissionSaveParam();
        param.setPermissionId(permissionId);
        param.setName(name);
        param.setParentId(parentId);
        param.setTag(tag);
        param.setUrl(url);
        param.setType(type);
        param.setUsable(usable);
        param.setOrderNum(orderNum);
        return permissionService.save(param);
    }

    @RequestMapping("/toDel")
    @ResponseBody
    public CommonResult<Void> toDel(Integer permissionId){
        PermissionToDelParam param = new PermissionToDelParam();
        param.setPermissionId(permissionId);
        return permissionService.toDel(param);
    }


    @RequestMapping("/treePage")
    public String treePage(){
        return "/member/permissionTree";
    }

    @RequestMapping("/getTree")
    @ResponseBody
    public CommonResult<List<PermissionQueryForTreeVo>> getTree(){

        CommonResult<List<PermissionQueryForTreeVo>> result = permissionService.queryForTree();
        return result;
    }
}
